/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package InternetHax;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.lcdui.Image;

/**
 *
 * @author LoginError
 */
public class ResourceManager {

    Hashtable resourceTable;

    //this class is a placeholder, since the Hashtable won't let me insert nulls
    private class Resource {

        public int numUses;
        public Image image;
        public String filename;

        public Resource() {
            numUses = 0;
            image = null;
            filename = "";
        }

        public boolean isLoaded() {
            return (image != null);
        }

        public String toString() {
            return filename + " : " + (image == null ? "not loaded" : "loaded " + numUses + " times");
        }
    }

    public ResourceManager() throws GameBreakingException {
        init();
    }

    public void init() throws GameBreakingException {

        String resourceList = Toolbox.getStringFromFile(this, "/Images.txt");
        if (resourceList != null) {
            String[] filenames = Toolbox.splitString(resourceList, "\n");
            if (filenames != null) {
                resourceTable = new Hashtable(filenames.length);
                for (int i = 0; i < filenames.length; i++) {
                    Resource res = new Resource();
                    res.filename = filenames[i];
                    resourceTable.put(filenames[i], res);
                }
            }
        } else {
            throw new GameBreakingException("Could not initialize ResourceManager");
        }
    }

//returns the requested resource, loading it from disk if it hasn't already been
    public Image getResourceByName(String filename) {
        Resource res = null;

        if (resourceTable.isEmpty()) {
            return null;
        } else {
            res = (Resource) resourceTable.get(filename);
        }
        if (!res.isLoaded()) { //hasn't been loaded yet

            try {
                res.image = Image.createImage("/" + filename);
            } catch (IOException ex) {
                ex.printStackTrace();
                return null;
            }

        }
        res.numUses++;
        resourceTable.put(filename, res);
        Toolbox.printDebug("Loading "+ filename + " : "+ res.numUses +" in use");
        return res.image;
    }

    public void unloadResource(String filename) {
        Resource res = (Resource) resourceTable.get(filename);
        
        if (res != null) {
            res.numUses--;
            Toolbox.printDebug("Unloading " +filename+ " : " +res.numUses + " remaining instances in use");
            if (res.numUses <= 0){ //no more referances to this resource exist
                res.image = null;// set it to null so garbage collection can get rid of it
                Toolbox.printDebug(res.filename + " is no longer in use");
            }
            resourceTable.put(filename, res);
        }
    }

    public void unloadResource(Image imageToRemove) {
        //go through all the resources to find the one with this image in it,
        Enumeration e = resourceTable.elements();
        while (e.hasMoreElements()) {
        
            Resource res = (Resource) e.nextElement();
            if (res.image == imageToRemove) {
                res.numUses--;
                Toolbox.printDebug("Unloading " + res.filename + " : " +res.numUses + " remaining instances in use");
                if(res.numUses <= 0)
                {
                    res.image = null;
                    Toolbox.printDebug(res.filename + " is no longer in use");
                }
                resourceTable.put(res.filename, res);
                return;
            }
        }
    }

    public void printResources() {
        System.out.println("-------");

        for (Enumeration e = resourceTable.elements(); e.hasMoreElements();) {
            Resource res = (Resource) e.nextElement();

            System.out.println(res);

        }

        System.out.println("-------");
    }
}
